package utils

import (
	"encoding/base64"

	"golang.org/x/crypto/bcrypt"
)

//密码加密
func Passwordencryption(password string)(hashedPasswordString string,err error){

	// 生成密码哈希
	hashedPasswordbyte, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "",err
	}

	// 将密码哈希转换为Base64编码的字符串
	hashedPasswordString = base64.StdEncoding.EncodeToString(hashedPasswordbyte)
	return hashedPasswordString,nil
	
}
// 密码验证
func Cipherdecoding(password string, hashedPasswordString string) bool {
	// 将 Base64 编码的密码字符串解码为原始的哈希字节
    hashedPassword, err := base64.StdEncoding.DecodeString(hashedPasswordString)
    if err != nil {
        // 解码失败，返回密码验证失败
        return false
    }
    // 比较密码哈希
    err = bcrypt.CompareHashAndPassword(hashedPassword, []byte(password))
    if err == nil {
        // 密码匹配
        return true
    } else {
        // 密码不匹配
        return false
    }
}
